package com.xncoding.scheduleDynamic.controller;

import com.xncoding.scheduleDynamic.dao.ISysJobRepository;
import com.xncoding.scheduleDynamic.jobs.CronTaskRegistrar;
import com.xncoding.scheduleDynamic.jobs.SchedulingRunnable;
import com.xncoding.scheduleDynamic.model.SysJobPO;
import com.xncoding.scheduleDynamic.util.SysJobStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class IndexController {
    private static final Logger _logger = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    ISysJobRepository sysJobRepository;
    @Autowired
    CronTaskRegistrar cronTaskRegistrar;

    /**
     * 主页
     *
     * @param model
     * @return
     */
    @RequestMapping({"/index"})
    public ModelAndView index(ModelAndView model) {
        model.addObject("msg", "welcome211啊啊e you!");
        model.setViewName("index");
        return model;
    }
    /**
     * 主页
     *
     * @return
     */
    @RequestMapping({"/"})
    public String index() {
        return "welcome211啊啊e you!";
    }

    @RequestMapping("/add")
    public String add(@RequestBody SysJobPO sysJob) {
        boolean success = sysJobRepository.saveAndFlush(sysJob) != null ;
        if (!success)
            return "新增失败";
        else {
            if (sysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
                cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
            }
        }
        return "";
    }

    @RequestMapping("/edit")
    public String edit(@RequestBody SysJobPO sysJob) {
        SysJobPO existedSysJob = new SysJobPO();
//        boolean success = sysJobRepository.editSysJob(sysJob);
        boolean success = sysJobRepository.findByJobId(sysJob.getJobId()) !=null;
        sysJobRepository.save(sysJob);
        if (!success)
            return "编辑失败";
        else {
            //先移除再添加
            if (existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
                cronTaskRegistrar.removeCronTask(task);
            }

            if (sysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
                cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
            }
        }

        return "";
    }

    @RequestMapping("/del")
    public String del(@RequestParam("id") String id) {
        SysJobPO existedSysJob = sysJobRepository.findByJobId(id);
        if (null == existedSysJob)
            return "删除失败";
        else{
            if (existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
                SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
                cronTaskRegistrar.removeCronTask(task);
            }
        }

        return "";
    }

    @RequestMapping("/puse")
    public String puse(ModelAndView model) {
        SysJobPO sysJob = new SysJobPO();
        SysJobPO existedSysJob = new SysJobPO();
        if (existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
            SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
            cronTaskRegistrar.addCronTask(task, existedSysJob.getCronExpression());
        } else {
            SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
            cronTaskRegistrar.removeCronTask(task);
        }
        return "";
    }



}
